package com.feng.mainPack;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Iterator;

import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.util.Units;
import org.apache.poi.xwpf.usermodel.BreakType;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFParagraph;
import org.apache.poi.xwpf.usermodel.XWPFRun;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import com.feng.net.utils.NetUtils;

public class GetContent extends Thread {

	@Override
	public void run() {

		while (QueueList.ArticleUrls.size() > 0) {

			// 一直下载

			String downUrl = QueueList.ArticleUrls.poll();

			// 放入队列
			QueueList.VisitedArticleUrls.offer(downUrl);
			// 下载
			try {
				get(downUrl, QueueList.downloadPath);
			} catch (Exception e) {
				System.out.println("下载失败，跳过");
				e.printStackTrace();
			}


			System.out.println("剩余Article" + QueueList.ArticleUrls.size());

		}

	}

	public void get(String url, String path) {

		// 先连接
		Document doc = null;
		try {
			doc = Jsoup.connect(url).timeout(300000).get();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		File f = new File(path);

		if (!f.exists()) {
			f.mkdirs();
		}

		NetUtils net = new NetUtils();

		// doc

		XWPFDocument docx = new XWPFDocument();
		XWPFParagraph p = docx.createParagraph();

		XWPFRun head = p.createRun();
		head.setFontSize(20);

		XWPFRun r = p.createRun();

		r.setFontFamily("宋体");
		r.setFontSize(12);

		XWPFRun urlFont = p.createRun();

		urlFont.setItalic(true);// 设置斜体

		urlFont.setText("文章中包含的链接：");
		urlFont.addCarriageReturn();

		// r.setText(imgUrl);
		// r.addBreak();
		// r.addPicture(new FileInputStream(imgUrl), format, imgUrl,
		// Units.toEMU(200), Units.toEMU(200)); // 200x200 pixels
		// r.addBreak(BreakType.PAGE);

		String title = doc.title();

		System.out.println("开始下载" + title);
		System.out.println(url);

		head.setText(title);
		head.addCarriageReturn();
		head.setText(url);
		head.addCarriageReturn();
		head.addCarriageReturn();
		head.addCarriageReturn();

		Element e = doc.getElementById("cnblogs_post_body");

		Elements params = e.getAllElements();

		Iterator<Element> ie = params.iterator();

		ie.next();// 获取这个elements里面使所有的元素

		while (ie.hasNext()) {
			Element e1 = ie.next();
			String name = e1.tagName();
			if (name != "p" && name != "img" && name != "div") {
				continue;
			}

			String html = e1.html();

			String text = e1.text();

			// r.addBreak();
			r.setText(text);
			// r.setItalic(true);//设置为斜体
			r.addCarriageReturn();
			// r.setCharacterSpacing(2);
			r.addBreak(BreakType.TEXT_WRAPPING);

			String imgUrl = null;
			String aUrl = null;

			if (html.contains("img")) { // 获取图片链接
				Elements es2 = e1.getElementsByTag("img");
				if (es2.size() > 0) {
					for (int i = 0; i < es2.size(); i++) {
						// 输出图片链接
						Element e2 = es2.get(i);
						//将图片格式全部设置为小写，防止下面报错
						imgUrl = e2.attr("src").toLowerCase();
						System.out.println(imgUrl);
						if (!imgUrl.contains("http://")) {
							break;
						}

						int w = 100;
						int h = 100;

						if (e2.attr("width") != null && !e2.attr("width").equals("")) {
							w = Integer.parseInt(e2.attr("width"));
						}
						if (e2.attr("height") != null && !e2.attr("height").equals("")) {
							h = Integer.parseInt(e2.attr("height"));
						}

						InputStream is = null;
						try {
							is = net.getEntity(imgUrl, null).getContent();
						} catch (UnsupportedOperationException | IOException e4) {
							System.out.println("下载图片失败");
							e4.printStackTrace();

						}

						if (is != null) {
							// 将图片写入
							int format;

							if (imgUrl.endsWith(".emf"))
								format = XWPFDocument.PICTURE_TYPE_EMF;
							else if (imgUrl.endsWith(".wmf"))
								format = XWPFDocument.PICTURE_TYPE_WMF;
							else if (imgUrl.endsWith(".pict"))
								format = XWPFDocument.PICTURE_TYPE_PICT;
							else if (imgUrl.endsWith(".jpeg") || imgUrl.endsWith(".jpg"))
								format = XWPFDocument.PICTURE_TYPE_JPEG;
							else if (imgUrl.endsWith(".png"))
								format = XWPFDocument.PICTURE_TYPE_PNG;
							else if (imgUrl.endsWith(".dib"))
								format = XWPFDocument.PICTURE_TYPE_DIB;
							else if (imgUrl.endsWith(".gif"))
								format = XWPFDocument.PICTURE_TYPE_GIF;
							else if (imgUrl.endsWith(".tiff"))
								format = XWPFDocument.PICTURE_TYPE_TIFF;
							else if (imgUrl.endsWith(".eps"))
								format = XWPFDocument.PICTURE_TYPE_EPS;
							else if (imgUrl.endsWith(".bmp"))
								format = XWPFDocument.PICTURE_TYPE_BMP;
							else if (imgUrl.endsWith(".wpg"))
								format = XWPFDocument.PICTURE_TYPE_WPG;
							else {
								System.err.println("Unsupported picture: " + imgUrl
										+ ". Expected emf|wmf|pict|jpeg|png|dib|gif|tiff|eps|bmp|wpg");
								continue;
							}

							try {
								// r.addBreak();
								r.addPicture(is, format, imgUrl, Units.toEMU(w), Units.toEMU(h));
								// r.addBreak(BreakType.PAGE);
							} catch (InvalidFormatException | IOException e3) {
								System.out.println("添加图片失败");
								// TODO Auto-generated catch block
								e3.printStackTrace();
							}

						}

					}
				}
			} else if (html.contains("href=\"")) { // 获取a标签
				Elements es2 = e1.getElementsByTag("a");
				if (es2.size() > 0) {
					for (int i = 0; i < es2.size(); i++) {
						// 输出图片链接
						Element e2 = es2.get(i);

						aUrl = e2.attr("href") + "\n";

						urlFont.setText(e2.text() + ":" + aUrl);
						urlFont.addCarriageReturn();// 换行
						// r.addBreak(BreakType.TEXT_WRAPPING);
					}
				}
			}

		} // end while

		try {
			FileOutputStream out = new FileOutputStream(path + "\\" + title.replace("/", "-").replace(":", "-") + ".docx");
			docx.write(out);
			out.close();
		} catch (IOException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}

		System.out.println("DONE");
	}

}
